package com.clkj.goldpartner.dao;

import com.clkj.goldpartner.pojo.GoodsStock;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * @author 杨行
 * @date 2021/3/3 17:12
 */
@Repository
public interface GoodsStockMapper extends Mapper<GoodsStock> {


    /**
     * 获取仓库列表
     *
     * @param shop_id
     * @param warehouse_id
     * @param in_price
     * @param category_id
     * @return
     */
    @Select("<script>" +
            "select * from t_cl_goodsStock a left join t_cl_goods b on a.goods_id = b.goods_id where a.shop_id = #{shop_id} " +
            "<if test=' 0!=warehouse_id '> and warehouse_id = #{warehouse_id} </if> " +
            "<if test=' 0!=in_price '> and in_price = #{in_price} </if> " +
            "<if test=' 0!=category_id '> and category_id = #{category_id} </if> " +
            "</script>")
    List<Map> getGoodsStock(Integer shop_id, Integer warehouse_id, Double in_price, Integer category_id);


    /**
     * 添加/修改商品库存
     *
     * @param shop_id
     * @param goods_id
     * @param goods_number
     * @param note
     * @param batch_number
     * @param production_date
     * @param expiration
     * @param warehouse_id
     * @param in_price
     */
    @Update("if exists (select * from t_cl_goodsStock where goods_id = #{goods_id} and shop_id = #{shop_id} " +
            "and batch_number = #{batch_number} and warehouse_id = #{warehouse_id} ) update t_cl_goodsStock " +
            "set goods_number = goods_number + #{goods_number} where goods_id = #{goods_id} and shop_id = #{shop_id} and batch_number = #{batch_number}" +
            " and warehouse_id = #{warehouse_id} " +
            "else insert into t_cl_goodsStock values (#{goods_id},#{goods_number},#{shop_id},NULL," +
            "#{batch_number},#{production_date},#{expiration},#{warehouse_id},#{in_price})")
    void addGoodsStock(Integer shop_id, Integer goods_id, Integer goods_number, String note, String batch_number, String production_date, String expiration, Integer warehouse_id, Double in_price);

    /**
     * 通过id获取库存列表
     *
     * @param ids
     * @return List<GoodsStock>
     */
    @Select("select * from t_cl_goodsStock where stock_id in (${ids})")
    List<GoodsStock> getGoodsStockById(@Param("ids") String ids);

    /**
     * 获取到期商品库存
     *
     * @param shop_id
     * @return
     */
    @Select("select * from t_cl_goodsStock a left join t_cl_goods b on a.goods_id = b.goods_id where a.shop_id = #{shop_id} and stock_id in (select stock_id from t_cl_goodsStock  where shop_id = #{shop_id} and  DATEDIFF(DAY,dateadd(MONTH,CAST(expiration as int),convert(datetime,production_date, 102)),GETDATE())>=-60)")
    List<Map> getGoodsStockWarn(Integer shop_id);

}